import router from './router';
import { getCookie } from '@/utils/support'
import { Message } from 'element-ui';
import store from './store'
//将不需要守卫的路由放到白名单
const whiteList = ['/login'];

//路由守卫
router.beforeEach((to, from, next) => {
  console.log("路由守卫触发了", to, from, next);
  if (getCookie("token")) {//已经登陆
    //如果路由是登录页默认跳转到首页
    if (to.path == "/login") {
      next("/home")
    } else if (to.path == "/super") {
      if (getCookie("roleid") == 1) {
        next()
      }
      else {
        Message({
          message: "您没有权限访问该页面，请退出后使用超级管理员身份登录",
          type: "error"
        });
        store.dispatch('LogoutAC');
        router.go(0)
      }
    }
    else {
      next()
    }
  }
  else {
    if (whiteList.indexOf(to.path) != -1) {
      //在白名单找到了路由
      next()
    }
    else {
      next("/login")
    }
  }
})